package com.cloudspace.jindun.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.constants.Constants;
import com.cloudspace.jindun.model.NoticePerson;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class NoticePersonAdatper extends BaseAdapter{
    private List<NoticePerson> data;

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(data)){
            return 0;
        }

        return data.size();
    }

    public void setData(List<NoticePerson> datas){
        if (ObjUtil.isEmpty(datas)){
            return;
        }

        this.data = datas;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(data)){
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_notice_person, null);
            holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.icon_iv = (ImageView) convertView.findViewById(R.id.image_iv);
            holder.title_tv= (TextView) convertView.findViewById(R.id.title_tv);
            holder.title_rl = (RelativeLayout) convertView.findViewById(R.id.title_rl);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NoticePerson noticePerson = data.get(position);
        NoticePerson preNoticePerson;


        if (position == 0){
            holder.title_rl.setVisibility(View.VISIBLE);

            if (TextUtils.equals(Constants.NOTICE_UNREAD_STATUS,noticePerson.status)){
                holder.title_tv.setText(JindunApp.getInstance().getResources().getString(R.string.unread_title));
            }else{
                holder.title_tv.setText(JindunApp.getInstance().getResources().getString(R.string.read_title));
            }
        }else{
            preNoticePerson = data.get(position-1);

            if (preNoticePerson != null){
                if (!TextUtils.equals(preNoticePerson.status, noticePerson.status)){
                    holder.title_rl.setVisibility(View.VISIBLE);
                    holder.title_tv.setText(JindunApp.getInstance().getResources().getString(R.string.read_title));
                }else{
                    holder.title_rl.setVisibility(View.GONE);
                }
            }
        }

        holder.name_tv.setText(noticePerson.person.getName());
        ImageManager.loadCircleImage(JindunApp.getInstance(),
                holder.icon_iv, noticePerson.person.getImageUrl(), Configs.IMAGE_CORNER_RADIUS);

        return convertView;
    }

    static class ViewHolder {
        TextView name_tv;
        ImageView icon_iv;
        TextView title_tv;
        RelativeLayout title_rl;
    }
}
